package com.xwj.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author xwj
 * @date 2020/5/9
 */
@RestController
public class OrderController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/productDelayOrder")
    public void productDelayOrder(String orderId) {
        System.out.println("订单：" + orderId + "创建成功，5秒内未完成支付将自动取消");
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(UUID.randomUUID().toString());
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        Message message = new Message(orderId.getBytes(), messageProperties);
        correlationData.setReturnedMessage(message);
        rabbitTemplate.convertAndSend("order-producer-exchange", "order-producer-routing-key", orderId, correlationData);
    }
}
